package com.sukaiyi.giteeideaplugin.component;

import com.intellij.icons.AllIcons;
import com.intellij.ui.JBColor;
import com.intellij.util.ui.AnimatedIcon;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

public class JSLoadingPanel extends JLayeredPane implements ComponentListener {

    private JPanel loadingPanel = null;
    private AnimatedIcon processIcon = null;
    private Component contentPanel = null;

    private static final Icon[] LOADING_ICONS = new Icon[]{
            AllIcons.Process.Big.Step_1,
            AllIcons.Process.Big.Step_2,
            AllIcons.Process.Big.Step_3,
            AllIcons.Process.Big.Step_4,
            AllIcons.Process.Big.Step_5,
            AllIcons.Process.Big.Step_6,
            AllIcons.Process.Big.Step_7,
            AllIcons.Process.Big.Step_8,
    };
    private static final Icon PASSIVE_ICON = AllIcons.Process.Big.Step_passive;
    public static final int CYCLE_LENGTH = com.intellij.ui.AnimatedIcon.Default.DELAY * LOADING_ICONS.length;


    public JSLoadingPanel() {
        loadingPanel = new TranslucenceJPanel(0.05f);
        loadingPanel.setOpaque(false);
        loadingPanel.setBackground(JBColor.BLACK);
        loadingPanel.setLayout(new BorderLayout());
        processIcon = new AnimatedIcon("loading", LOADING_ICONS, PASSIVE_ICON, CYCLE_LENGTH);
        loadingPanel.add(processIcon);
        super.add(loadingPanel, POPUP_LAYER);
        super.addComponentListener(this);

        loaded();
    }

//    @Override
//    public void add(@NotNull Component comp, Object constraints) {
//        if(contentPanel == null){
//            super.add(comp, DEFAULT_LAYER);
//            contentPanel = comp;
//        }
//    }

    public void loading() {
        processIcon.resume();
        loadingPanel.setVisible(true);
    }

    public void loaded() {
        processIcon.suspend();
        loadingPanel.setVisible(false);
    }

    @Override
    public void componentResized(ComponentEvent componentEvent) {
        loadingPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
        contentPanel.setBounds(0, 0, this.getWidth(), this.getHeight());
    }

    @Override
    public void componentMoved(ComponentEvent componentEvent) {

    }

    @Override
    public void componentShown(ComponentEvent componentEvent) {

    }

    @Override
    public void componentHidden(ComponentEvent componentEvent) {

    }
}
